#include "main_widget.h"
#include <QPushButton>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPdfWriter>
#include <poppler-qt5.h>
#include <QPdfWriter>
#include <QFile>
#include <QPainter>
#include <QPrinter>
#include <QMessageBox>
#include <QProgressBar>
#include <QFileDialog>
MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , m_btnSelectFile(new QPushButton("选择待处理文件目录",this))
    , m_edtSelectFile(new QLineEdit(this))
    , m_btnSaveFile(new QPushButton("选择待保存文件目录",this))
    , m_edtSaveFile(new QLineEdit(this))
    , m_layoutMain(new QVBoxLayout())
    , m_layoutSelect(new QHBoxLayout())
    , m_layoutSave(new QHBoxLayout())
    , m_btnDoWork(new QPushButton("生成",this))
    , m_progressBar(new QProgressBar(this))
{
    intiUI();
    initConnect();
}

void MainWidget::intiUI()
{
    m_layoutSelect->addWidget(m_edtSelectFile);
    m_layoutSelect->addWidget(m_btnSelectFile);

    m_layoutSave->addWidget(m_edtSaveFile);
    m_layoutSave->addWidget(m_btnSaveFile);

    m_layoutMain->addLayout(m_layoutSelect);
    m_layoutMain->addLayout(m_layoutSave);

    m_layoutMain->addWidget(m_btnDoWork);
    m_layoutMain->addWidget(m_progressBar);
    setLayout(m_layoutMain);
}

void MainWidget::initConnect()
{
    connect(m_btnSelectFile,&QPushButton::clicked,this,&MainWidget::slotSelectFileDir);
    connect(m_btnSaveFile,&QPushButton::clicked,this,&MainWidget::slotSaveFileDir);
    connect(m_btnDoWork,&QPushButton::clicked,this,&MainWidget::slotDoWork);
}

void MainWidget::slotSelectFileDir()
{
    QString fileName = QFileDialog::getOpenFileName(this,QStringLiteral("文件对话框！"),"/home/arv000/Desktop/语文+数学苏教版自主练习/数学自主练习（苏教版）",QStringLiteral("*.pdf"));
    m_edtSelectFile->setText(fileName);
}

void MainWidget::slotSaveFileDir()
{

}

void MainWidget::slotDoWork()
{

    QString filenamePath=m_edtSelectFile->text();
    if(filenamePath.isEmpty()){
        QMessageBox::information(this,"提示","未选择文件");
    }
    QFileInfo fileinfo =QFileInfo(filenamePath);
    QString fileName = fileinfo.fileName();
    Poppler::Document* document = Poppler::Document::load(filenamePath);
    m_progressBar->setValue(0);
    m_progressBar->setRange(0,document->numPages()-1);
    if (!document || !document->isLocked()) {

        QPrinter printer(QPrinter::HighResolution);
        printer.setPageSize(QPrinter::A4);
        printer.setResolution(300);
        printer.setOutputFormat(QPrinter::PdfFormat);
        QString outFile;
        outFile = QString::fromLocal8Bit("/home/arv000/Desktop/temp/")+fileName;
        printer.setOutputFileName(outFile);
        QPainter painter;
        painter.begin(&printer);
        painter.setRenderHint(QPainter::Antialiasing, true);
        for(int i = 0; i < document->numPages();i++){
             Poppler::Page* pdfPage = document->page(i);
             QImage image = pdfPage->renderToImage(300,300);
             QImage logoImage;
             logoImage.load("/home/arv000/Desktop/1.jpg");
             logoImage = logoImage.scaled(logoImage.width()/4,logoImage.height()/4);
             if(0 == i){
                 painter.drawImage(0,0,image);
                 painter.drawImage(1600,50,logoImage);
                 printer.newPage();
             }else{
                 painter.drawImage(0,0,image);
                 painter.drawImage(200,50,logoImage);
                 printer.newPage();
             }
            m_progressBar->setValue(i);
        }
        painter.end();
      delete document;
    }
    QMessageBox::information(this,"提示","更新完成");
}
